$(function () {
  const form = layui.form

  const laypage = layui.laypage

  const q = {
    tagname: '',
    page: 1,
    limit: 10
  }

  render()
  function render() {
    $.ajax({
      method: 'GET',
      url: '/ydd_tag/list',
      data: q,
      // headers: {
      //   "x-token": localStorage.getItem('token')
      // },
      success: function (res) {
        // console.log(res)
        const { data, errno } = res
        const { count, totalPages, pageSize, currentPage } = data

        if (errno !== 0) return layer.msg('获取失败')
        const zzww = data.data.map(function (item, i) {
          return `
          <tr>
  
  
  
          <td>${i + 1}</td>
          <td>${item.id}</td>
          <td>${item.tagname}</td>
          <td class="zzw">
            <button type="button" class="layui-btn layui-btn-normal layui-icon layui-icon-edit fzz"
              style="border-radius: 50%;" data-id="${item.id}"></button>
            <button type="button" class="layui-btn layui-btn-danger layui-icon layui-icon-delete qwe"
              style="border-radius: 50%;" data-id="${item.id}"></button>
          </td>
        </tr>
         `
        })
        $('.layui-table tbody').html(zzww.join(''))
        fenyeqi(count)
      }
    })
  }



  let layerIndex = null
  $('#add').on('click', function (e) {
    e.preventDefault()
    layerIndex = layer.open({
      type: 1,
      title: '添加标签',
      area: ['800px', '200px'],
      content: $('#zzzzz').html() //这里content是一个普通的String
    });
  })


  $('body').on('submit', '#form-add', function (e) {
    e.preventDefault()
    const tagname = $(this).serialize()
    $.ajax({
      method: 'POST',
      url: '/ydd_tag/add',
      data: tagname,
      success: function (res) {
        layer.close(layerIndex)
        if (res.errno !== 0) return layer.msg('获取失败')

        render()
      }
    })
  })


  $('.layui-table tbody').on('click', ".fzz", function () {
    // e.preventDefault()
    const id = $(this).data('id')

    $.ajax({
      method: 'GET',
      url: '/ydd_tag/rowInfo?id=' + id,

      success: function (res) {
        console.log(res.data.tagname);
        form.val('uzi', res.data)

      }
    })






    layerIndex = layer.open({
      type: 1,
      title: '修改标签',
      area: ['800px', '220px'],
      content: $('#jjjjj').html() //这里content是一个普通的String
    });
  })

  $('body').on('submit', '#form-aad', function (e) {
    e.preventDefault()

    // const id = $(this).data('id')

    // const tagname = $(this).serialize()
    $.ajax({
      method: 'POST',
      url: '/ydd_tag/update',
      data: $(this).serialize(),
      success: function (res) {
        layer.close(layerIndex)
        if (res.errno !== 0) return layer.msg('编辑失败')
        render()
      }
    })



  })

  $('.layui-table tbody').on('click', '.qwe', function () {
    const id = $(this).data('id')
    console.log("id", id)

    layer.confirm('确认删除?', { icon: 3, title: '提示' }, function (index) {
      //do something
      $.ajax({
        method: 'GET',
        url: `/ydd_tag/del`,
        data: {
          id: id
        },
        success: function (res) {
          console.log(res)
          if (res.errno !== 0) return layer.msg('删除失败')
          layer.msg('删除成功')
          render()
        }
      })
      layer.close(index);
    });
  })



  function fenyeqi(count) {
    laypage.render({
      elem: 'page-box',
      count: count,
      limit: q.limit,
      curr: q.page,
      layout: ['count', 'limit', 'prev', 'page', 'next'],
      limits: [5, 10, 15, 20],
      jump: function (obj, first) {
        //obj包含了当前分页的所有参数，比如：
        // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
        // console.log(obj.limit); //得到每页显示的条数

        q.page = obj.curr
        q.limit = obj.limit
        //首次不执行
        if (!first) {
          //do something
          render()
        }
      }
    })
  }

  $('body').on('submit', '#selectForm', function (e) {
    e.preventDefault()

    const sousuo = $('#input').val().trim()
    // console.log("sousuo", sousuo)


    q.tagname = sousuo
    render()



    $('#input').val('')
  })


  $('body').on('click', '.uuu', function () {
    layer.close(layerIndex)
  })

  $('body').on('click', '.kkk', function () {
    layer.close(layerIndex)
  })
})




